from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

service = Service(executable_path=r"E:\mypython2411\chromedriver.exe")
driver = webdriver.Chrome(service=service)

driver.get("https://quotes.toscrape.com/")

datas = []

while True:

	items = driver.find_elements(By.XPATH, '//div[@class="quote"]')
	for item in items:

		content = item.find_element(By.XPATH, './span[@class="text"]')
		author = item.find_element(By.XPATH, './/small[@class="author"]')
		# print(content.text, author.text)
		tags = item.find_elements(By.CLASS_NAME, 'tag')
		tag_text_list = []
		for tag in tags:
			# print(tag.text, tag.get_attribute("href"))
			tag_text_list.append(tag.text)

		datas.append({
			"id": len(datas) + 1,
			"content": content.text,
			"author": author.text,
			"tags": tag_text_list
		})

	try:
		next_ele = driver.find_element(By.XPATH, '//li[@class="next"]/a')
		if next_ele:
			next_ele.click()
	except NoSuchElementException as e:
		print(f"爬取结束")
		break

print(len(datas))
for data in datas:
	print(data)


driver.add_cookie()
